<?php

if (!defined('IN_IA')) {
    die('Access Denied');
}
global $_W, $_GPC;
$openid = m('user')->getOpenid();
$member = m('member')->getInfo($openid);
$op = empty($_GPC['op']) ? 'display' : $_GPC['op'];
if ($op == 'display') {
    include $this->template('franchise/list');
}
if ($op == 'product') {
    $id = intval($_GPC['id']);
    if (empty($_GPC['id'])) {
        exit;
    }
    //虚拟已加盟人数
    if ($id == 1) {
        $xu_num = 230;
    }
    if ($id == 2) {
        $xu_num = 330;
    }
    if ($id == 3) {
        $xu_num = 430;
    }
    if ($id == 4) {
        $xu_num = 530;
    }
    $real_num = pdo_fetchcolumn('select count(*) from ' . tablename('franchise_apply_order') . " where uniacid=:uniacid and shop_type=:shop_type ", array(':uniacid' => $_W['uniacid'], ':shop_type' => $id));
    $total = $xu_num + $real_num;
    include $this->template('franchise/product');
}
if ($op == 'confirm') {
    if (!empty($_GPC['data'])) {
        $ordersn = m('common')->createORDER('franchise_apply_order', 'ordersn', 'JM');
        $data = $_GPC['data'];
        $data['uniacid'] = $_W['uniacid'];
        $data['openid'] = $openid;
        if($_GPC['data']['type']==1){
        $data['price']=0.01;     
        }
        if($_GPC['data']['type']==2){
        $data['price']=0.02;  
        }
        $data['createtime']=time();
        $data['ordersn']=$ordersn;
        $record1=pdo_fetch('select * from '.tablename('franchise_apply_order').' where uniacid=:uniacid and type=:type and openid=:openid and status=1 and shop_type=:shop_type order by id desc ',array(':uniacid'=>$_W['uniacid'],':type'=>$_GPC['data']['type'],':openid'=>$openid,':shop_type'=>$data['shop_type']));
        $record2=pdo_fetch('select * from '.tablename('franchise_apply_order').' where uniacid=:uniacid and type=:type and openid=:openid and status=2 and shop_type=:shop_type order by id desc ',array(':uniacid'=>$_W['uniacid'],':type'=>$_GPC['data']['type'],':openid'=>$openid,':shop_type'=>$data['shop_type']));
        if($record1){
          show_json(-1, '您已购买，无需重复购买!');  
        }
        if($record2){
          show_json(-1, '您已开通，无需重复开通!');  
        }
        if(empty($record1) && empty($record2)){
        pdo_insert('franchise_apply_order', $data);
        $recordid = pdo_insertid();
        show_json(1,array('recordid'=>$recordid));
        die;
        }
    }
    include $this->template('franchise/confirm');
}
if ($op == 'experience_meeting') {
    $nowtime = time();
    if (empty($_GPC['type'])) {
        $meeting = pdo_fetchall('select * from ' . tablename('franchise_experience_meeting') . " where uniacid=:uniacid and status=1  order by id desc", array(':uniacid' => $_W['uniacid']));
    }
    if ($_GPC['type'] == 1) {
        $meeting = pdo_fetchall('select * from ' . tablename('franchise_experience_meeting_join_info') . "as j left join " . tablename('franchise_experience_meeting') . "as m on m.id=j.meet_id where m.uniacid=:uniacid and j.openid=:openid  order by m.id desc ", array(':uniacid' => $_W['uniacid'], ':openid' => $openid));
    }
//    if ($_GPC['type'] == 2) {
//        $meeting = pdo_fetchall('select m.* from ' . tablename('franchise_experience_meeting') . "as m left join " . tablename('franchise_experience_meeting_join_info') . "as j on m.id=j.meet_id where m.uniacid=:uniacid and j.openid=:openid and starttime<'{$nowtime}' order by m.id desc ", array(':uniacid' => $_W['uniacid'], ':openid' => $openid));
//    }
    include $this->template('franchise/experience_meeting');
}
if ($op == 'meeting_detail') {
    if (empty($_GPC['meeting_id'])) {
        exit;
    }
    $meeting = pdo_fetch("select * from " . tablename('franchise_experience_meeting') . "where uniacid=:uniacid and id=:id", array(':uniacid' => $_W['uniacid'], ':id' => $_GPC['meeting_id']));
    $meeting['total_num'] = $meeting['join_num'] + $meeting['em_num'];
    $info = pdo_fetch('select * from ' . tablename('franchise_experience_meeting_join_info') . " where uniacid=:uniacid and openid=:openid and meet_id=:meet_id", array(':uniacid' => $_W['uniacid'], ':openid' => $openid, ':meet_id' => $meeting['id']));
    if (!empty($info)) {
        $meeting['join'] = true;
    } else {
        $meeting['join'] = false;
    }
    include $this->template('franchise/meeting_detail');
}
if ($op == "join_meeting") {
    if (empty($_GPC['meeting_id'])) {
        exit;
    }
    $meeting = pdo_fetch("select * from " . tablename('franchise_experience_meeting') . "where uniacid=:uniacid and id=:id", array(':uniacid' => $_W['uniacid'], ':id' => $_GPC['meeting_id']));
    if (!empty($_GPC['data'])) {
        $data = $_GPC['data'];
        $data['uniacid'] = $_W['uniacid'];
        $data['meet_id'] = $_GPC['meeting_id'];
        $data['openid'] = $openid;
        $data['createtime'] = time();
        pdo_insert('franchise_experience_meeting_join_info', $data);
        $recordid=pdo_insertid();
        if(!empty($recordid)){
        //报名人数加一
        $join_num=$meeting['join_num']+1;
        pdo_update("franchise_experience_meeting",array("join_num"=>$join_num),array('uniacid' => $_W['uniacid'], 'id' =>$meeing['id']));
        //发生报名成功的推送消息！
        $msg = array(
        'first' => array('value' => "您已成功报名{$meeting['title']}体验会！", "color" => "#4a5077"),
        'time' => array('title' => '会议时间', 'value' => date("Y-m-d H:i",$meeting['starttime']), "color" => "#4a5077"),
        'address' => array('title' => '会议地点', 'value' =>$meeting['address'], "color" => "#4a5077"),
        'remark' => array('value' => "请记得按时参加哦~更多资讯尽在小妖家!", "color" => "#4a5077")
        );
        m('message')->sendCustomNotice($openid, $msg);
        show_json(1);
        }else{
        show_json(0);    
        }
    }
    include $this->template('franchise/join_meeting');
}